package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.system.domain.StringId;
import com.ruoyi.system.domain.comment.FileComment;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BEFile;
import com.ruoyi.system.service.IBEFileService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文件Controller
 * 
 * @author ruoyi
 * @date 2024-05-08
 */
@RestController
@RequestMapping("/system/file")
public class BEFileController extends BaseController
{
    @Autowired
    private IBEFileService bEFileService;

    /**
     * 查询文件列表
     */
//    @PreAuthorize("@ss.hasPermi('system:file:list')")
    @GetMapping("/list")
    public TableDataInfo list(BEFile bEFile)
    {
        startPage();
        List<BEFile> list = bEFileService.selectBEFileList(bEFile);
        return getDataTable(list);
    }

    /**
     * 导出文件列表
     */
//    @PreAuthorize("@ss.hasPermi('system:file:export')")
    @Log(title = "文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BEFile bEFile)
    {
        List<BEFile> list = bEFileService.selectBEFileList(bEFile);
        ExcelUtil<BEFile> util = new ExcelUtil<BEFile>(BEFile.class);
        util.exportExcel(response, list, "文件数据");
    }


    /**
     * 查询文件注释信息列表
     *
     * @return 文件信息集合
     */
    @PostMapping("/getFileCommentInfo")
    public List<FileComment> getFileCommentInfo() {
        return bEFileService.selectBeFileComInfoList();
    }

    /**
     * 查询文件信息通过id
     */
    @PostMapping("/getFileById")
    public AjaxResult getFileInfoById(@RequestBody StringId stringId) {
        if (stringId.getIds() != null) {
            String[] stringList = stringId.getIds();
            String id = stringList[0];
            return success(bEFileService.selectBEFileById(id));
        } else {
            String id = stringId.getId();
            return success(bEFileService.selectBEFileById(id));
        }
    }

    /**
     * 获取二进制文件
     */
    @PostMapping("/getPdf")
    public ResponseEntity<byte[]> getPdf(@RequestBody BEFile File) {
        return bEFileService.getpdf(File.getId());
    }

    /**
     * 获取文件类型
     */
//    @PostMapping("/getType")
//    public String getType(@RequestBody BEFile File) {
//        return bEFileService.getType(File.getId());
//    }

    /**
     * 获取文件详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:file:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(bEFileService.selectBEFileById(id));
    }

    /**
     * 新增文件
     */
//    @PreAuthorize("@ss.hasPermi('system:file:add')")
    @Log(title = "文件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BEFile bEFile)
    {
        return toAjax(bEFileService.insertBEFile(bEFile));
    }

    /**
     * 修改文件
     */
//    @PreAuthorize("@ss.hasPermi('system:file:edit')")
    @Log(title = "文件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BEFile bEFile)
    {
        return toAjax(bEFileService.updateBEFile(bEFile));
    }

    /**
     * 删除文件
     */
//    @PreAuthorize("@ss.hasPermi('system:file:remove')")
    @Log(title = "文件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(bEFileService.deleteBEFileByIds(ids));
    }

    @GetMapping("downloadMore/{ids}")
    public ResponseEntity<byte[]> downloadMore(@PathVariable String[] ids) {
        return bEFileService.downloadMore(ids);
    }
}
